package
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.*;
	public class ControlPanel extends Sprite
	{
		private var playbutton: PlaButton;
	private var resetbutton: ResetButton;
	private var stepforwardbutton: StepForwardButton;
	private var _stepforward: Boolean = false;
	private var stepbackbutton: StepBackButton
	private var _stepback: Boolean = false;
	private var _pause:Boolean = true;
	private var _reset:Boolean = false;
	private var scale:Number;
	private var matrix:Matrix;
	private var panelwidth:Number = 615;
	private var panelheight:Number = 350;
	private var panelSlider1: HorizontalSlider;

	private var panel:Sprite;

	public function ControlPanel()
	{
		init();
	}

	private function init():void
	{
		panel = new Sprite();
		addChild(panel);
		matrix = new Matrix;
		playbutton = new PlaButton();
		addChild(playbutton);
		playbutton.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverPlay);
		playbutton.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutPlay);
		playbutton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownPlay);
		playbutton.addEventListener(MouseEvent.MOUSE_UP, onMouseUpPlay);
		resetbutton = new ResetButton();
		addChild(resetbutton);
		resetbutton.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverReset);
		resetbutton.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutReset);
		resetbutton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownReset);
		resetbutton.addEventListener(MouseEvent.MOUSE_UP, onMouseUpReset);
		stepbackbutton = new StepBackButton();
		addChild(stepbackbutton);
		//addEventListener(Event.ENTER_FRAME, onEnterFrame);
		stepbackbutton.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverstepback);
		stepbackbutton.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutstepback);
		stepbackbutton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownstepback);
		stepbackbutton.addEventListener(MouseEvent.MOUSE_UP, onMouseUpstepback);

		stepforwardbutton = new StepForwardButton();
		addChild(stepforwardbutton);
		//addEventListener(Event.ENTER_FRAME, onEnterFrame);
		stepforwardbutton.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverstepforward);
		stepforwardbutton.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutstepforward);
		stepforwardbutton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownstepforward);
		stepforwardbutton.addEventListener(MouseEvent.MOUSE_UP, onMouseUpstepforward);
		//addEventListener(Event.ENTER_FRAME, onEnterFrame);

		panelSlider1 = new HorizontalSlider(1,20,1,.1,true);
		addChild(panelSlider1);
		this.draw();
	}


	private function drawbar():void
	{
		//First, add the control panel area
		matrix.createGradientBox(panelwidth, 0.1*panelheight, Math.PI/2,  0, 0);
		panel.graphics.beginGradientFill(GradientType.LINEAR, [0xeeeeee, 0xcccccc], [1,1], [0,255], matrix);
		panel.graphics.lineStyle(0, 0xAAAAAA, 1);
		panel.graphics.moveTo(0, 0);
		panel.graphics.lineTo(panelwidth,0);
		panel.graphics.lineTo(panelwidth, 0.09*panelheight);
		panel.graphics.curveTo(panelwidth, .1*panelheight, 0.99*panelwidth, .1*panelheight);
		panel.graphics.lineTo(0.01*panelwidth, .1*panelheight);
		panel.graphics.curveTo(0, .1*panelheight, 0, 0.09*panelheight);
		panel.graphics.lineTo(0, 0)
		panel.graphics.endFill();
		panel.graphics.lineStyle(0, 0x000000, 1);
		trace(panelheight,panelwidth);
		panel.x = 0;
		panel.y = 0;

	}
	public function draw():void
	{
		drawbar();
		panelSlider1.width = .2*panelwidth;
		panelSlider1.x = .4*panelwidth;
		panelSlider1.y = 0.012*panelheight;
		panelSlider1.height= .077*panelheight;
		panelSlider1.SlValTFX = .3*panelwidth;
		panelSlider1.SlValTFY = .001*panelheight;
		panelSlider1.backColor = 0xFFFF00;
		playbutton.y = 0.01*panelheight;
		playbutton.x = 0.008*panelwidth;
		scale = .1*panelheight;

		playbutton.draw(scale, _pause, true, false);
		// The parameters are: scale, _pause, button up, button red. _pause true is the playbutton,
		//_pause false the pause button.

		//addEventListener(Event.ENTER_FRAME, onEnterFrame);

		resetbutton.x = playbutton.x + 0.98*scale;
		resetbutton.y = playbutton.y;
		resetbutton.draw(scale, true, false);
		// the parameters are scale, up, red.



		stepbackbutton.x = resetbutton.x + 1*scale;
		stepbackbutton.y = playbutton.y;

		stepbackbutton.draw(scale, true, false);
		// The parameters are: scale, _pause, button up, button red. _pause true is the stepbackbutton,
		//_pause false the pause button.

		stepforwardbutton.x = stepbackbutton.x + .8*scale;
		stepforwardbutton.y = playbutton.y;

		stepforwardbutton.draw(scale, true, false);
		// The parameters are: scale, _pause, button up, button red. _pause true is the stepforwardbutton,
		//_pause false the pause button.

	}

	//Mouse functions for Play button:

	public function onMouseOverPlay(event:MouseEvent):void
	{

		playbutton.draw(scale, _pause, true, true);

	}
	public function onMouseOutPlay(event:MouseEvent):void
	{

		playbutton.draw(scale, _pause, true, false);

	}
	public function onMouseDownPlay(event:MouseEvent):void
	{

		playbutton.draw(scale, _pause, false, true);

	}
	public function onMouseUpPlay(event:MouseEvent):void
	{
		if(_pause)
		{
			_pause = false;
			playbutton.draw(scale, _pause, true, true);
		}
		else
		{
			_pause = true;
			playbutton.draw(scale, _pause, true, true);
		}
	}

	//Mouse functions for Reset button:

	public function onMouseOverReset(event:MouseEvent):void
	{
		resetbutton.draw(scale, true, true);
	}
	public function onMouseOutReset(event:MouseEvent):void
	{
		resetbutton.draw(scale, true, false);
	}

	public function onMouseUpReset(event:MouseEvent):void
	{
		resetbutton.draw(scale, true, true);
		_reset = false;
	}
	public function onMouseDownReset(event:MouseEvent):void
	{
		resetbutton.draw(scale, false, true);
		_reset = true;
		_pause = false;
		playbutton.draw(scale, _pause, true, true);
	}

	//Mouse functions for stepback button:

	public function onMouseOverstepback(event:MouseEvent):void
	{
		stepbackbutton.draw(scale, true, true);
	}
	public function onMouseOutstepback(event:MouseEvent):void
	{
		stepbackbutton.draw(scale, true, false);
	}

	public function onMouseUpstepback(event:MouseEvent):void
	{
		stepbackbutton.draw(scale, true, true);
		_stepback = false;
	}
	public function onMouseDownstepback(event:MouseEvent):void
	{
		stepbackbutton.draw(scale, false, true);
		_pause = true;
		_stepback = true;
		playbutton.draw(scale, _pause, true, true);
	}

	//Mouse functions for stepforward button:

	public function onMouseOverstepforward(event:MouseEvent):void
	{
		stepforwardbutton.draw(scale, true, true);
	}
	public function onMouseOutstepforward(event:MouseEvent):void
	{
		stepforwardbutton.draw(scale, true, false);
	}

	public function onMouseUpstepforward(event:MouseEvent):void
	{
		stepforwardbutton.draw(scale, true, true);
		_stepforward = false;
	}
	public function onMouseDownstepforward(event:MouseEvent):void
	{
		stepforwardbutton.draw(scale, false, true);
		_pause = true;
		_stepforward = true;
		playbutton.draw(scale, _pause, true, true);
	}

//	functions to read and set _pause and _reset
	public function get pause():Boolean
	{
		return _pause;
	}
	public function set pause(n:Boolean):void
	{
		_pause = n;
		playbutton.draw(scale, _pause, true, false);
	}
	public function get reset():Boolean
	{
		return _reset;
	}

	public function get stepforward():Boolean
	{
		return _stepforward;
	}

	public function get stepback():Boolean
	{
		return _stepback;
	}
	public function get slider1():Number
	{
		return panelSlider1.value;
	}
	public function set _panelheight(n:Number):void
	{
		panelheight = n;
		draw();
	}
	}
}
